#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./login/bkaccount/utils.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import StringIO, xlrd, xlwt
from django.http import JsonResponse
from common.log import logger
from bkaccount.constants import ApiErrorCodeEnum

def _gen_json_response(result, code, message, data=None):
    if not data:
        data = {}
    return JsonResponse({'result': result,'code': code,'message': message,'data': data})


def gen_fail_json(code, message, data=None):
    return _gen_json_response(False, code, message, data)


def gen_success_json(message, data=None):
    return _gen_json_response(True, ApiErrorCodeEnum.SUCCESS, message, data)


def read_user_import_xls(xls_file):
    str_file = StringIO.StringIO(xls_file.read())
    wbk = xlrd.open_workbook(file_contents=str_file.read())
    sheet = wbk.sheets()[0]
    user_list = []
    for i in range(sheet.nrows - 1, 0, -1):
        user_list.append({'username': sheet.row_values(i)[0],
           'chname': sheet.row_values(i)[1],
           'qq': sheet.row_values(i)[2],
           'phone': sheet.row_values(i)[3],
           'email': sheet.row_values(i)[4]
           })

    return user_list


def write_excel(head_list, records):
    wbk = xlwt.Workbook(encoding='gbk')
    sheet = wbk.add_sheet('Sheet1')
    for i in range(0, 22):
        sheet.col(i).width = 5328

    fnt = xlwt.Font()
    fnt.name = 'Arial'
    fnt.colour_index = 4
    fnt.bold = True
    style = xlwt.XFStyle()
    style.font = fnt
    for i, data in enumerate(head_list):
        sheet.write(0, i, data)

    try:
        for index, record in enumerate(records):
            for cell_index, cell_value in enumerate(record):
                sheet.write(index + 1, cell_index, cell_value)

    except Exception as e:
        logger.exception('export excel fail!')
        return (
         False, u'导出数据出现错误:%s' % e, wbk)

    return (
     True, 'SUCCESS', wbk)


def get_page_info(page, page_size, default_page_size=10):
    try:
        page = int(page)
        page_size = int(page_size)
        if page < 1:
            page = 1
        if page_size < 1:
            page_size = default_page_size
    except:
        page = 1
        page_size = default_page_size

    return (
     page, page_size)
